﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdEditPro.xaml
    /// </summary>
    
    public partial class wdEditPro : Window
    {
        String idProject;
        clsProject project;
        public wdEditPro( String ID)
        {
            idProject = ID;
            InitializeComponent();
        }
        public void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void OK_Click(object sender, RoutedEventArgs e)
        {
            int id;
            project = new clsProject();
            int.TryParse(idProject, out id);
            project.ID = id;
            project.ProName = proName.Text;
            project.ProPM = proPM.Text;
            project.ProCustomer = proCus.Text;
            project.ProStartDay = proStartDay.SelectedDate;
            project.ProEndDay = proEndDay.SelectedDate;
            project.ProNote = new TextRange(proNote.Document.ContentStart, proNote.Document.ContentEnd).Text;
            //MessageBox.Show(project.ProStartDay.ToString());
            Nullable<DateTime> tam;
            DateTime?[] progressDate = new DateTime?[6];
            progressDate[0] = proProgress1.SelectedDate;
            progressDate[1] = proProgress2.SelectedDate;
            progressDate[2] = proProgress3.SelectedDate;
            progressDate[3] = proProgress4.SelectedDate;
            progressDate[4] = proProgress5.SelectedDate;
            progressDate[5] = proProgress6.SelectedDate;
            // chuyen gia tri null ve sau
            for (int i = 0; i <= 5; i++)
            {
                if (progressDate[i] == null)
                    for (int j = i + 1; j <= 5; j++)
                    {
                        if (progressDate[j] != null)
                        {
                            progressDate[i] = progressDate[j];
                            progressDate[j] = null;
                            break;
                        }
                    }
            }
            //sap xep theo gia tri tang dan
            for (int i = 0; i < 6; i++)
                for (int j = 0; j < 6; j++)
                    if (progressDate[i] < progressDate[j])
                    {
                        tam = progressDate[i]; progressDate[i] = progressDate[j]; progressDate[j] = tam;
                    }
            //gan gia tri cua cac mang vao cac bien trong class clsProject
            project.ProProgress1 = progressDate[0];
            project.ProProgress2 = progressDate[1];
            project.ProProgress3 = progressDate[2];
            project.ProProgress4 = progressDate[3];
            project.ProProgress5 = progressDate[4];
            project.ProProgress6 = progressDate[5];
            //kiem tra tinh dung dan
            if (project.ProName == "")
                MessageBox.Show("Project name can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProEndDay <= project.ProStartDay)
            {
                MessageBox.Show("End Day must be greater Start Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);

            }
            else if (project.ProStartDay == null)
                MessageBox.Show("Start Day can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProEndDay == null)
                MessageBox.Show("End Day can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (project.ProProgress1 <= project.ProStartDay || project.ProProgress1 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 1 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (project.ProProgress2 <= project.ProStartDay || project.ProProgress2 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 2 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (project.ProProgress3 <= project.ProStartDay || project.ProProgress3 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 3 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (project.ProProgress4 <= project.ProStartDay || project.ProProgress4 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 4 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (project.ProProgress5 <= project.ProStartDay || project.ProProgress5 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 5 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            else if (project.ProProgress6 <= project.ProStartDay || project.ProProgress6 >= project.ProEndDay)
            {
                MessageBox.Show("Progress 6 must be greater Start Day and less than End Day !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
            //thuc hien insert
            else
            {
                project.updateProject();
                MessageBox.Show("You have update success !!!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                //Update Progress
                clsProgress progress = new clsProgress();
                progress.IdProject = id;
                progress.Progress1 = progressDate[0];
                progress.Progress2 = progressDate[1];
                progress.Progress3 = progressDate[2];
                progress.Progress4 = progressDate[3];
                progress.Progress5 = progressDate[4];
                progress.Progress6 = progressDate[5];
                progress.Plan1 = 0;
                progress.Actual1 = 0;
                progress.Plan2 = 0;
                progress.Actual2 = 0;
                progress.Plan3 = 0;
                progress.Actual3 = 0;
                progress.Plan4 = 0;
                progress.Actual4 = 0;
                progress.Plan5 = 0;
                progress.Actual5 = 0;
                progress.Plan6 = 0;
                progress.Actual6 = 0;
                progress.updateProgress();
            } 
        }
    }
}
